package com.example.demo.game;

import com.example.demo.DemoApplication;
import com.example.demo.model.Userinfo;
import com.example.demo.service.UserService;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.UUID;

@Data
//代表一个人机房间类对象
public class MachineRoom {
    private String roomId;

    private Userinfo userinfo1;

    private Integer whitestatr;//先手状态
    @Autowired
    private GetUserLoginMatch getUserLoginMatch;
    private ObjectMapper objectMapper=new ObjectMapper();

    private MachineChess machineChess;
    @Autowired
    private RoomManager roomManager;
    private UserService userService;
    public MachineRoom() {
        roomId= UUID.randomUUID().toString();
        getUserLoginMatch=DemoApplication.context.getBean(GetUserLoginMatch.class);
        machineChess=new MachineChess();
    }
    /**
     * 处理一次真人落子请求
     * @param payload
     * @throws IOException
     */
    public void putchess(String payload) throws IOException {
        ReqGame reqGame =objectMapper.readValue(payload,ReqGame.class);//获取前端传来的落子请求
        RespMachine respMachine =new RespMachine();
        if(!reqGame.getMessage().equals("putChess")) {
            System.out.println("不符合落子请求的数据格式");
            respMachine.setMessage("error");
            return;
        }
        System.out.println("服务器收到落子请求");
        int chess=1;
        int row=reqGame.getRow();//行
        int col=reqGame.getCol();//列
        int userid=reqGame.getUserid();//落子的用户id
        int marow=0;
        int macol=0;
        int winner=0;
        boolean isLegal = machineChess.isLegal(row, col);//判断是否合法落子
        if(isLegal) {
            machineChess.addChessman(row, col, 1);//逻辑业务方面加一个棋子

            if(machineChess.isWin(row, col, 1)){//判断真人是否胜利
                 winner=userinfo1.getUserid();
            }

            Location loc = machineChess.searchLocation();
            macol=loc.getY();
            marow=loc.getX();
            //人机落子
            machineChess.addChessman(loc.getX(), loc.getY(), loc.getOwner());

            //判断机器是否胜利
            if(machineChess.isWin(loc.getX(), loc.getY(), -1)){
                winner=-1;
            }
        }
        machineChess.printBoard();
        System.out.println("行:"+row+"列:"+col);
        System.out.println();
        WebSocketSession socketSession1=getUserLoginMatch.getFromGameGame(userinfo1.getUserid());//获取当前登录用户的登录信息
        if(socketSession1==null) {//说明用户一掉线了
            winner=-1;//掉线直接设置对方胜利
            System.out.println("玩家掉线");
        }
        respMachine.setMessage("putChess");
        respMachine.setRow(row);
        respMachine.setCol(col);
        respMachine.setMarow(marow);
        respMachine.setMacol(macol);
        respMachine.setWinner(winner);
        respMachine.setUserid(userid);
        System.out.println(winner);
        System.out.println("当前落子为:"+chess);
        if(socketSession1!=null) {
            socketSession1.sendMessage(new TextMessage(objectMapper.writeValueAsString(respMachine)));
        }
        //!=0说明已经分出胜负,可以销毁房间和用户连接了
        if(winner!=0) {
            System.out.println("胜负已分");
            roomManager.removeMachineRoot(roomId,userinfo1);
            getUserLoginMatch.exitGameGame(userid);
//            roomManager.remove(roomId,userinfo1);
        }
    }
}
